/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GenericPanel.java
 *
 * Created on 1/03/2011, 08:16:42 PM
 */

package org.itver.common.propertyeditors.visual;

import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.Serializable;

/**
 *
 * @author Karo
 */
public class GenericPanel extends javax.swing.JPanel implements Serializable{

//    /** Creates new form GenericPanel */
//    public GenericPanel(String propertyName){
//        property.setText(propertyName);
//        initComponents();
//    }

    public GenericPanel() {
        initComponents();
        this.propertyText.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                final float STEP = 0.5f;
                String text = propertyText.getText();
                if(!(text.matches("\\-?[0-9]+(\\.[0-9]+)?")))
                    return;
                double value = Double.valueOf(text);
                switch(e.getKeyCode()){
                    case KeyEvent.VK_UP:
                        if(e.isShiftDown())
                            value += STEP * 2;
                        else
                            value += STEP;
                        propertyText.setText("" + value);
                        break;
                    case KeyEvent.VK_DOWN:
                        if(e.isShiftDown())
                            value -= STEP * 2;
                        else
                            value -= STEP;
                        propertyText.setText("" + value);
                }
                
            }
            
        });
        
        this.propertyText.addFocusListener(new FocusAdapter() {

            @Override
            public void focusLost(FocusEvent e) {
                String text = propertyText.getText();
                if(!(text.matches("\\-?[0-9]+(\\.[0-9]+)?")))
                    propertyText.setText("0");
            }            
        });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        property = new javax.swing.JLabel();
        propertyText = new javax.swing.JTextField();

        setPreferredSize(new java.awt.Dimension(530, 25));
        setLayout(new java.awt.BorderLayout(10, 10));

        property.setText(org.openide.util.NbBundle.getMessage(GenericPanel.class, "GenericPanel.property.text")); // NOI18N
        add(property, java.awt.BorderLayout.LINE_START);

        propertyText.setText(org.openide.util.NbBundle.getMessage(GenericPanel.class, "GenericPanel.propertyText.text")); // NOI18N
        add(propertyText, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    public void addListener(FocusListener listener){
        this.propertyText.addFocusListener(listener);
    }
    
    public void removeListener(FocusListener listener){
        this.propertyText.removeFocusListener(listener);
    }

    public void setPropertyName(String propName){
        property.setText(propName);
    }

    public void setPropertyValue(String value){
        propertyText.setText(value);
    }

    public String getPropertyName(){
        return property.getText();
    }

    public String getPropertyValue(){
        return propertyText.getText();
    }

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel property;
    private javax.swing.JTextField propertyText;
    // End of variables declaration//GEN-END:variables

}
